package com.view.imageview;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.widget.ImageView;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * Created by Administrator on 2016-12-19.
 */
public class DownloadImgUtils {
    /**
     * 根据url下载图片到指定的文件夹
     */
    public static boolean downloadImagByUrl(String urlStr, File file){
        FileOutputStream fos = null;

        InputStream is = null;
        try {
            URL url = new URL(urlStr);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();

            is = conn.getInputStream();
            fos = new FileOutputStream(file);

            byte[] buf = new byte[512];
            int len = 0;

            while ((len = is.read(buf)) != -1){
                fos.write(buf,0,len);
            }
            fos.flush();
            return true;

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                if (is != null){
                    is.close();
                }

                if (fos !=null ){
                    fos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }


        return false;
    }

    /**
     * 根据url下载图片到指定的文件夹
     *
     * @param urlStr
     * @param imageView
     * @return
     */
    public static Bitmap downloadImagByUrl(String urlStr, ImageView imageView){

        FileOutputStream fos = null;
        InputStream is = null;


        try {
            URL url = new URL(urlStr);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            is = new BufferedInputStream(conn.getInputStream());
            is.mark(is.available());

            BitmapFactory.Options opts = new BitmapFactory.Options();
            opts.inJustDecodeBounds = true;

            Bitmap bitmap = BitmapFactory.decodeStream(is,null,opts);

            //获取imageView想要显示的宽和高
            ImageSizeUtil.ImageSize imageSize = ImageSizeUtil.getImageViewSize(imageView);
            opts.inSampleSize = ImageSizeUtil.caculateInSampleSize(opts,imageSize.width,imageSize.height);

            opts.inJustDecodeBounds = false;
            is.reset();

            bitmap = BitmapFactory.decodeStream(is,null,opts);
            conn.disconnect();
            return bitmap;
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                if (is != null){
                    is.close();
                }

                if (fos !=null){
                    fos.close();
                }


            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }



}
